$(function(){
	if(!IS_AUTH){
		$_GET['user_id'] = ME_ID;
	}
	if(!$_GET['page']) $_GET['page'] = 1;

	var fileListDiv = $('#file-list-div'),
		ListMenu = { //列表页面菜单
			'排序': {
				'按最新排序': function(){
					$_GET = $.extend($_GET, {page: 1, orderby: 'file_time', sequence: 'desc'});
					location.search = '?'+$.param($_GET);
				},
				'按 ID 排序': function(){
					$_GET = $.extend($_GET, {page: 1, orderby: 'file_id', sequence: 'asc'});
					location.search = '?'+$.param($_GET);
				}
			},
			'上一页': function(){
				$_GET['page'] -= 1;
				location.search = '?'+$.param($_GET);
			},
			'下一页': function(){
				$_GET['page'] += 1;
				location.search = '?'+$.param($_GET);
			},
			'修改': {
				'编辑': function(){
					var target = fileListDiv.find('.active').eq(0),
						id = target.data('id'),
						mediaBody = target.find('.media-body'),
						fileName = mediaBody.children('h4').text().replace(/\s*/g, ''),
						fileDesc = mediaBody.children('p').eq(0).text().replace(/\s*/g, ''),
						modal = $('#file-modal');
					if(target.length != 1){
						alert('请选择需要编辑的评论。')
					}else{
						modal.one('shown.bs.modal', function(){
							$('#file_name').focus();
						}).modal('show').find('form').find('input[name="file_id"]').val(id);
						$('#file_name').val(fileName);
						$('#file_desc').val(fileDesc);
					}
				},
				'删除': function(){
					fileListDiv.editMulti({
						obj: 'file',
						act: 'delete',
						unit: '个',
						action: '删除',
						item: '文件'
					});
				}
			}
		},
		showListMenu = function(){ //显示列表菜单
			if(!TOTAL || TOTAL == 1){
				var menu = null;
			}else if($_GET['page'] == 1 && $_GET['page'] == PAGES){
				var menu ={'排序': ListMenu['排序']};
			}else if($_GET['page'] == 1){
				var menu ={
					'排序': ListMenu['排序'],
					'下一页': ListMenu['下一页']
				};
			}else if($_GET['page'] == PAGES){
				var menu ={
					'排序': ListMenu['排序'],
					'上一页': ListMenu['上一页']
				};
			}else{
				var menu = {
					'排序': ListMenu['排序'],
					'上一页': ListMenu['上一页'],
					'下一页': ListMenu['下一页']
				}
			}
			var length = fileListDiv.find('.active').length;
			if(length){
				if(menu == null){
					menu = $.extend({}, ListMenu['修改']);
				}else{
					menu['修改'] = $.extend({}, ListMenu['修改']);
					if(length > 1) delete menu['修改']['编辑'];
				}
			}
			$.showMenu(menu);
		};

	showListMenu(); //显示列表菜单

	/** 点击列表项时对其进行高亮 */
	fileListDiv.on('click', '.media', function(){
		$(this).toggleClass('active');
		showListMenu();
	});

	/** 提交编辑文件的表单 */
	$('#file-modal form').ajaxSubmit({
		success: function(result){
			if(result.success){
				var target = fileListDiv.find('.active').eq(0).find('.media-body');
				target.children('h4').text(result.data.file_name);
				target.children('p').eq(0).text(result.data.file_desc);
				$('#file-modal').modal('hide');
			}else{
				alert(result.data);
			}
		},
		error: function(xhr){
			alert('与服务器的连接出现错误，文件修改失败。');
			console.log(xhr.responseText);
		}
	});

	/** 查看大图 */
	$('.media-logo img').click(function(event){
		event.stopPropagation();
		_$('#help-info').slideUp('fast');
		var modal = $('#file-modal-picture'),
			src = $(this).attr('src').replace('_64.', '.'),
			ext = src.getExt(),
			fileName = $(this).closest('.media').find('h4.media-heading').text();
		modal.find('.modal-title').text(fileName);
		modal.find('img').attr('src', src);
		modal.modal('show');
	});

	$.showHelp('你可以通过点击缩略图来查看原始图像。');
})